function appPost(url, postdata) {
    var result = {};

    postdata.timestamp = (new Date()).valueOf();
    $.ajax({
        type: "POST",
        url: url,
        data: postdata,
        dataType: "json",
        async: false,
        success: function (data) {
            result = data;
        }
    });
    return result;
}

function appGet(url, getdata) {
    var result = {};
    getdata.timestamp = (new Date()).valueOf();
    $.ajax({
        type: "GET",
        url: url,
        data: getdata,
        dataType: "json",
        async: false,
        success: function (data) {
            result = data;
        }
    });
    return result;
}


function add_to_cart(saleid) {
    var res = appPost('add_car', {saleid: saleid, count: 1});

    if (res.status != true) {
        alert(JSON.stringify(res));
    }
    //alert(saleid)
    if ($("#myConfirm").length > 0) {
        $("#myConfirm").remove();
    }
    var html = "<div class='modal fade' id='myConfirm' >"
        + "<div class='modal-backdrop in' style='opacity:0; '></div>"
        + "<div class='modal-dialog' style='z-index:2901; margin-top:60px; width:400px; '>"
        + "<div class='modal-content'>"
        + "<div class='modal-header'  style='font-size:16px; '>"
        + "<span >News </span><button type='button' class='close' data-dismiss='modal'>"
        + "<span style='font-size:20px;  ' class='glyphicon glyphicon-remove'></span><tton></div>"
        + "<div class='modal-body text-center' id='myConfirmContent' style='font-size:18px; '><span class='glyphicon  glyphicon-check'></span>"
        + "Successfully added to cart!" + "</div>"
        + "<div class='modal-footer ' style=''>"
        + "<a href='car' class='btn btn-danger ' >To pay</a>"
        + "<button class='btn btn-info ' data-dismiss='modal'> Wait a minute</button>"
        + "</div>" + "</div></div></div>";
    $("body").append(html);


    $("#myConfirm").modal("show");


    // $("#confirmOk").on("click", function() {
    // 	$("#myConfirm").modal("hide");
    // 	alert('66')
    // });

}


function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

function search(obj) {
    var e = window.event || arguments.callee.caller.arguments[0];

    var params = $(obj).val().trim();
    if (e && e.keyCode == 13 && params != '') { // enter 键
        window.location.href = 'search?params=' + params;
    }
}

$('label[name=checkbox]').click(function () {
    var red = 'rgb(255, 0, 0)';
    var c = $(this).css('background-color');
    if (c != red) {
        $(this).css('background-color', red);
        $('#strong_num').html(parseInt($('#strong_num').html()) + 1);
        $('#strong_total').html((parseFloat($('#strong_total').html()) + parseFloat($(this).attr('total'))).toFixed(2));
        var carids = $('#carids').val().split(',');
        carids.push($(this).attr('carid'));
        $('#carids').val(carids.join(','))
    } else {
        $(this).css('background-color', 'rgba(0, 0, 0, 0)');
        $('#strong_num').html(parseInt($('#strong_num').html()) - 1);
        $('#strong_total').html((parseFloat($('#strong_total').html()) - parseFloat($(this).attr('total'))).toFixed(2));
        var carids = $('#carids').val().replace($(this).attr('carid'));
        $('#carids').val(carids)
    }
});


$('#settle').click(function () {
    if ($('#strong_num').html() == '0') {
        return '';
    }

    var label_carid = $('label[name=checkbox]');
    var carid = $('#carids').val();
    var objs = $('input[name=settle_info]');

    var data = {carid: carid, total: $('#strong_total').html()};
    for (var i=0;i<objs.length;i++) {
        var obj = objs[i];
        var id = $(obj).attr('id');
        console.log(id);
        var value = $(obj).val().trim();
        if (value.length == 0) {
            alert_msg(id + 'cannot be empty');
            return;
        } else {
            data[id] = value;
        }
    }
    var res = appPost('create_order', data);
    if (res.status == true) {
        alert_msg('Order generated successfully');
    } else {
        alert_msg(JSON.stringify(res));
    }
});

function alert_msg(msg) {
    if ($("#myConfirm").length > 0) {
        $("#myConfirm").remove();
    }
    var html = "<div class='modal fade' id='myConfirm' >"
        + "<div class='modal-backdrop in' style='opacity:0; '></div>"
        + "<div class='modal-dialog' style='z-index:2901; margin-top:60px; width:400px; '>"
        + "<div class='modal-content'>"
        + "<div class='modal-header'  style='font-size:16px; '>"
        + "<span >News </span><button type='button' class='close' data-dismiss='modal'>"
        + "<span style='font-size:20px;  ' class='glyphicon glyphicon-remove'></span><tton></div>"
        + "<div class='modal-body text-center' id='myConfirmContent' style='font-size:18px; '><span>"+msg+"</span></div>"
        + "<div class='modal-footer ' style=''>"
        + "<button class='btn btn-info ' data-dismiss='modal'> confirm</button>"
        + "</div>" + "</div></div></div>";
    $("body").append(html);


    $("#myConfirm").modal("show");

}

$('#check_all').click(function () {
    var red = 'rgb(255, 0, 0)';
    var all_color=$(this).css('background-color');
    var objs=$('label[name=checkbox]');
debugger;
    if(all_color!=red){
        for(var i=0;i<objs.length;i++) {
            var obj = objs[i];
            var c = $(obj).css('background-color');
            if (c != red) {
                $(obj).click();
            }
        }
        $(this).css('background-color', red);
    }else{
        for(var i=0;i<objs.length;i++) {
            var obj = objs[i];
            var c = $(obj).css('background-color');
            if (c == red) {
                $(obj).click();
            }
        }
        $(this).css('background-color', 'rgba(0, 0, 0, 0)');
    }


});